import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiDutyStatusRecordsApi } from '#/api/moxi/aiDutyStatusRecords';
// 获取所有部门
import { getDingdDeptInfoPage } from '#/api/moxi/dingddeptinfo';
import { handleTree } from '@vben/utils';

import { z } from '#/adapter/form';
import { getDeptList } from '#/api/system/dept';
import { getSimplePostList } from '#/api/system/post';
import {
    CommonStatusEnum,
    DICT_TYPE,
    getDictOptions,
} from '#/utils';
import Dayjs from 'dayjs';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'username',
      label: '用户名称',
      component: 'Input',
      rules: 'required',
    },
    {
      label: '用户密码',
      fieldName: 'password',
      component: 'InputPassword',
      rules: 'required',
      dependencies: {
        triggerFields: ['id'],
        show: (values) => !values.id,
      },
    },
    {
      fieldName: 'nickname',
      label: '用户昵称',
      component: 'Input',
      rules: 'required',
    },
    {
      fieldName: 'deptId',
      label: '归属部门',
      component: 'ApiTreeSelect',
      componentProps: {
        api: async () => {
          const data = await getDeptList();
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择归属部门',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'postIds',
      label: '岗位',
      component: 'ApiSelect',
      componentProps: {
        api: getSimplePostList,
        labelField: 'name',
        valueField: 'id',
        mode: 'multiple',
        placeholder: '请选择岗位',
      },
    },
    {
      fieldName: 'email',
      label: '邮箱',
      component: 'Input',
      rules: z.string().email('邮箱格式不正确').or(z.literal('')).optional(),
    },
    {
      fieldName: 'mobile',
      label: '手机号码',
      component: 'Input',
    },
    {
      fieldName: 'sex',
      label: '用户性别',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_USER_SEX, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(1),
    },
    {
      fieldName: 'status',
      label: '用户状态',
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
    return [
        {
            fieldName: 'name',
            label: '员工姓名',
            component: 'Input',
            componentProps: {
                placeholder: '请输入员工姓名',
                allowClear: true,
            },
        },
        {
            fieldName: 'ofDate',
            label: '查询时间',
            component: 'DatePicker',
            // defaultValue: dayjs().format('YYYY-MM-DD'), // 初始值为字符串格式
            componentProps: {
                picker: 'date',
                allowClear: true,
                valueFormat: 'YYYY-MM-DD',
                format: 'YYYY-MM-DD',
            },
        },

    ];
}

/** 列表的字段 */
export function useGridColumns<T = AiDutyStatusRecordsApi.AiDutyStatusRecords>(
    onStatusChange?: (
        newStatus: number,
        row: T,
    ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
    return [
        {
            field: 'id',
            title: '序号',
        },
        {
            field: 'name',
            title: '员工姓名',
            formatter: ({ row }) => {
                return row ? row?.employee.name : '';
            },
        },
        // {
        //     field: 'userid',
        //     title: '员工Id'
        // },
        {
            field: 'deptNames',
            title: '所属部门 ',
            formatter: ({ row }) => {
                return row?.employee?.deptNames?.split(',').pop()?.trim() || '';
            },
        },
        {
            field: 'startTime',
            title: '在岗开始时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm') : '';
            },
        },
        {
            field: 'endTime',
            title: '在岗结束时间',
            formatter: ({ cellValue }) => {
                return cellValue ? Dayjs(cellValue).format('YYYY-MM-DD HH:mm') : '';
            },
        },
        {
            field: 'mobile',
            title: '在岗时长',
            formatter: ({ row }) => {
                return Dayjs(row.endTime).diff(Dayjs(row.startTime), 'hour') + 'h'
            }
        },
        {
            title: '操作',
            width: 180,
            fixed: 'right',
            slots: { default: 'actions' },
        },
    ];
}
